function e(e) {
    a.onShowData || (a.onShowData = {}), a.onShowData.scene = e;
}

var t = require("../../api.js"), a = getApp();

Page({
    data: {
        selected: -1,
        message_info: []
    },
    onLoad: function(e) {
        a.pageOnLoad(this, e);
        var n = this;
        wx.showLoading({
            title: "加载中"
        }), a.request({
            url: t.recharge.list,
            success: function(e) {
                var t = e.data;
                t.balance && 0 != t.balance.status || wx.showModal({
                    title: "提示",
                    content: "充值功能未开启，请联系管理员！",
                    showCancel: !1,
                    success: function(e) {
                        e.confirm && wx.navigateBack({
                            delta: 1
                        });
                    }
                }), n.setData(e.data);
            },
            complete: function(e) {
                wx.hideLoading();
            }
        });
    },
    onReady: function() {
        a.pageOnReady(this);
    },
    onShow: function() {
        a.pageOnShow(this);
    },
    onHide: function() {
        a.pageOnHide(this);
    },
    onUnload: function() {
        a.pageOnUnload(this);
    },
    click: function(e) {
        this.setData({
            selected: e.currentTarget.dataset.index
        });
    },
    pay: function (n) {
        var that = this;
        if (typeof wx.requestSubscribeMessage === "function"){
            var tmplIdsInfo = [];
            for (var key in that.data.message_info) {
                tmplIdsInfo.push(that.data.message_info[key])
            }
            wx.requestSubscribeMessage({
                tmplIds: tmplIdsInfo,
                success: function (res) {
                },
                fail: function (res) {
                    console.log(res);
                },
                complete: function (res) {
                    that.pay_submit(n)
                }
            })
        }else {
            this.pay_submit(n)
        }
        
    },
    pay_submit: function(n) {
        var i = this, o = {}, c = i.data.selected;
        if (-1 == c) {
            var s = i.data.money;
            if (s < .01) return void wx.showModal({
                title: "提示",
                content: "充值金额不能小于0.01",
                showCancel: !1
            });
            o.pay_price = s, o.send_price = 0;
            if(i.data.balance && i.data.balance.send_integral > 0) {
                o.send_integral =parseInt((s * (i.data.balance.send_integral/100)).toFixed());
            } else {
                o.send_integral = 0;
            }
        } else {
            var d = i.data.list;
            o.pay_price = d[c].pay_price, o.send_price = d[c].send_price;

            if(d[c].send_integral > 0) {
                o.send_integral = d[c].send_integral
            } else {
                if(i.data.balance && i.data.balance.send_integral > 0) {
                    o.send_integral =parseInt((s * (i.data.balance.send_integral/100)).toFixed());
                } else {
                    o.send_integral = 0;
                }
            }

        }
        o.pay_price ? (o.pay_type = "WECHAT_PAY", wx.showLoading({
            title: "提交中"
        }), a.request({
            url: t.recharge.submit,
            data: o,
            method: "POST",
            success: function(t) {
                if (0 == t.code) return setTimeout(function() {
                    wx.hideLoading();
                }, 1e3), e("pay"), void wx.requestPayment({
                    _res: t,
                    timeStamp: t.data.timeStamp,
                    nonceStr: t.data.nonceStr,
                    package: t.data.package,
                    signType: t.data.signType,
                    paySign: t.data.paySign,
                    success: function(e) {},
                    fail: function(e) {},
                    complete: function(e) {
                        "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? wx.showModal({
                            title: "提示",
                            content: "充值成功",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function(e) {
                                e.confirm && wx.navigateBack({
                                    delta: 1
                                });
                            }
                        }) : wx.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认"
                        });
                    }
                });
                wx.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1
                }), wx.hideLoading();
            }
        })) : wx.showModal({
            title: "提示",
            content: "请选择充值金额",
            showCancel: !1
        });
    },
    input: function(e) {
        this.setData({
            money: e.detail.value
        });
    }
});